This example demonstrates the use of physically-based shading and HDR post-processing in Kanzi. Since physically-based shading and HDR post-processing are still in the development phase, use these examples as a sneak peak into a technology you will be able to use in a later version of Kanzi.
Key features:
You can find the examples that show the use of physically-based materials in these example projects:
Advanced materials with post-processing requires either OpenGL ES 3.x, or OpenGL ES 2.0 with a set of extensions. The following OpenGL ES extensions are required when using OpenGL ES 2.0:
If these extensions are not available, it is still possible to use physically-based materials and lights, however, currently post-processing is not available without these extensions.
To use post-processing, set in the Project > the Target Graphics API property to at least OpenGL ES 2.0 Extension Pack.
Post-processing requires these main steps:
To use post-processing you need to set up these resources:
Resources. Resources, such as textures, composers, render passes, material types, shaders, materials, custom property types are needed from Post_Processing example project.
You also need the "Prefabs/Quad Scene" prefab. This scene prefab contains only one, specially prepared object: a quad with extents from (-1, -1) to (1, 1). The quad has a tag "Quad" which is used in post-processing composers to specify object source for render passes that apply the post-processing effect to a texture. Do not modify this prefab.
There are two versions of the example project. Post_Processing_Simple is nearly minimal setup, and Post_Processing has extra pages to show the scene with and without post processing, and to show intermediate textures.
The example projects are prepared for target resolution of 1920x1080. The debug version of the example project has resources to support a few other resolutions.
The main scene prefab template. The main scene must come from a prefab. In the example project this prefab is called "Prefabs/Main Scene". This is where you place content you want to render with post-processing.
To support physically-based material shading, the scene prefab must have these properties:
Additionally, the scene prefab template must have "Prefab preview mode" property set to 1. This value is intended to be overridden in the placeholders. This way, when previewing the prefab placeholder composition (value 1), material shaders perform additional tone mapping. Normally tone mapping is done after (or at the end of) post processing (value 0).
For detailed descriptions of these properties, see Property Types.
Viewport for the "Scene to Texture". The node from the previous step ("Page") must contain a viewport for rendering scene to texture. In the Post_Processing_Simple example project this viewport is named "Page/Scene to Texture".
The viewport must have the Layer Aspect Ratio property set to a value that matches the aspect ratio of the target resolution. In the example project the target resolution is 1920x1080 with aspect ratio of 1.777.
The viewport should have an alias to provide an easy access to the Layer Aspect Ratio property. In the example project the resource dictionary of the "Screen" has the "Viewport" alias.
Scene placeholder for the "Scene to Texture". The viewport from the previous step ("Page/Scene to Texture") must have a scene placeholder which instantiates the "Main Scene" prefab template from the step ("Prefabs/Main Scene"). In the example project this scene placeholder is named "Scene to Texture/Scene (Main Scene)".
The scene placeholder must have the Composer property with value "Render to Texture (1920x1080) Composer".
The scene placeholder must have the "Prefab Preview Mode" property set to 0. This directs the material shader to operate normally (without extra tone mapping).
This node renders the contents of the main scene to the "HDR Render 1920x1080" render target texture, which is later used as input for post-processing operations.
Providing access to the "Inverse Exposure" property. The Preview composition for the Main Scene prefab template requires the value of the exposure value for tone mapping. Because prefabs cannot access anything outside the prefab, this property is stored in the prefab, and made available to the project using the prefab placeholder.
The scene placeholder from the previous step ("Scene (Main Scene)") must have an alias, to provide easy access to exposure property. In the example project, the resource dictionary of the Screen has the "MainScene" alias that points to the scene placeholder.
The node from step 3 ("Page") must have this binding for the "Inverse Exposure" property:
{#MainScene/Exposure}
Post-processing nodes that need the exposure setting get it from the node from step 3 ("Page") through this binding and property inheritance.
Scene placeholder for "Post Processing Intermediate". The viewport from the previous step ("Page/Post Processing Intermediate") must contain a scene placeholder for the "Prefabs/Quad Scene" prefab (from step 1). In the example project, this is the "Post Processing Intermediate/Scene (Quad Scene)".
The scene placeholder must have the Composer property set to "Post Processing (1920x1080) Intermediate Composer".
The shaders that create the bloom in this composer need these properties to control the bloom effect:
These are inheritable properties and they come from the node in step 3 ("Page").
The viewport must have a binding for the Inverse Exposure property so that it uses value from the scene:
- {Inverse Exposure} = {../Scene/Exposure}
For detailed descriptions of these properties, see Property Types.
Scene placeholder for "Final Composition". The viewport from step 9 ("Page/Final Composition") should contain a scene placeholder for "Prefabs/Quad Scene" prefab (from step 1). In the example project, this scene is named "Final Composition/Scene (Quad Scene)".
The scene placeholder must have the Composer property set to "Post Processing (1920x1080) Final Composer".
The shader that creates the bloom composition and tone mapping needs these properties:
These are inheritable properties and they must come from the node in step 3 ("Page").
For detailed descriptions of these properties, see Property Types.
Note that you must not modify these textures.
Post Processing (1920x1080) Intermediate Composer. This composer reads the HDR texture and produces a set of blurred and scaled textures from it.
The composer has alternating horizontal and vertical render passes. Each render pass performs a blur filter in one dimension, reading from the input texture, and rendering to the output texture, which measures half the input in the specified dimension.
The first pass reads from the HDR Render texture, and writes to a render target texture which best matches size of the HDR render texture with width halved.
Physically-based lights properties:
Light Color Scale. Physically based materials will multiply light color with the light color scale value. This can be used to create lights with high intensities.
Note that light color scale only applies to the physically based shader. It is not recommended to mix physically based and non-physically based material shaders that involve lights.
Post-processing (bloom effect and tone mapping) properties:
Environment properties:
Material properties:
Base Color Map (optional) texture modulates base color, when material type supports base color map.
A white texture works as if there was no Base Color Map, however, using white texture is less efficient than creating a material type with Base Color Map disabled.
Using Base Color Map requires that material type supports texture coordinates, and that mesh has texture coordinates (kzTextureCoordinate0).
RoughnessMapScale (optional). When a material type supports Surface texture, the value of RoughnessMapScale * value of the roughness channel from Surface texture is added to the value of Roughness property. Scale can be either negative or positive.
If you have roughness texture where shiny parts are white, and rough parts are black, start by setting Roughness to 1.0 and RoughnessMapScale to -1.0.
Surface texture (optional). When a material type supports the Surface texture, color channels in at are used as Roughness, Cavity, Metalness and Ambient occlusion.
A black texture works as if there was no Base Color Map, however, using black texture is less efficient than creating a material type with surface texture disabled.
Using surface texture requires that material type supports texture coordinates, and that mesh has texture coordinates (kzTextureCoordinate0).
Normal Map (optional). When material type supports normal map, this property selects a normal map that is applied to the surface.
Using tangent texture requires texture coordinates (kzTextureCoordinate0) and tangents.
Using texture with flat color (0.5, 0.5, 1.0) works as if there was no normal map. However, this is less efficient than creating a material type with surface texture disabled.
Anisotropy (optional). When material type supports anisotropic light reflections, absolute value of anisotropy specifies amount of anisotropy, and sign specifies direction. Anisotropic light reflections provide look of brushed metals.
Using anisotropic light reflections requires that mesh has tangents. You can generate tangents for any mesh that has normals and texture coordinates with Generate Tangents context menu for Mesh.
Anisotropy value of 0.0 means surface is fully isotropic however, using 0.0 anisotropy is less efficient than creating a material type with anisotropy disabled.
Tangent Map (optional). When material type supports tangent textures, tangents can be sourced from a texture, instead of vertex attribute.
Using tangent texture requires texture coordinates (kzTextureCoordinate1).
Using tangent texture voids issues with singularities such as in circularly brushed metal.
All material types in this folder use identical shader sources for physically-based material. The material types are customized with Preprocessor Defines property. The main purpose of various preprocessor defines is to allow creation of optimized shader variants. In theory, it might be possible to use one, or only few, variants of the physically-based material type. In practice, it may be necessary to create more variants of physically-based material type to reach better performance.
To create a new variant, duplicate an existing material type, adjusted preprocessor defines to minimum of required features, and click Sync with Uniforms.
The shader uses the following preprocessor defines:
KANZI_SHADER_USE_AMBIENT_OCCLUSION modulates environmental light such as skylight. Ambient occlusion can come from either vertex color, or from alpha channel of surface texture.
0 disables support for ambient occlusion
1 enables support for ambient occlusion.
KANZI_SHADER_USE_AMBIENT_OCCLUSION_VERTEX_ATTRIBUTE. See also KANZI_SHADER_USE_AMBIENT_OCCLUSION.
0 selects Surface texture as source for ambient occlusion
1 selects vertex color as source for ambient occlusion
KANZI_SHADER_USE_ANISOTROPY
0 disables support for anisotropic light reflections and makes the shader faster.
1 enables support for anisotropic light reflections and slows down the shader.
KANZI_SHADER_USE_BASECOLOR_TEXTURE
0 disables support for basecolor texture and makes the shader faster.
1 enables support for basecolor texture and slows down the shader.
KANZI_SHADER_USE_CAMERA_EXPOSURE controls whether material shader performs camera exposure.
Note that KANZI_SHADER_USE_PREFAB_PREVIEW preprocessor define allows material type to control camera exposure (and linear to sRGB conversion) with a uniform, enabling camera exposure even if KANZI_SHADER_USE_CAMERA_EXPOSURE is set to 0.
0 disables camera exposure during material shading. Use this setting when using post-processing.
1 enables camera exposure during material shading. Use this setting when not using post-processing.
KANZI_SHADER_USE_NORMALMAP_TEXTURE. Normalmap requires texture coordinates and tangents.
0 enables the use of normalmap.
1 disables the use of normalmap.
KANZI_SHADER_USE_PREFAB_PREVIEW
0 disables use of prefab preview and makes the shader faster.
1 enables use of prefab preview, which allows bypassing post processing, and slows down the shader.
KANZI_SHADER_USE_SKY_LIGHT
0 disables environmental skylight approximation.
1 enables environmental skylight approximation.
KANZI_SHADER_USE_SURFACE_TEXTURE you can use to modulate material properties Roughness, Metallic, Cavity and Ambient Occlusion.
0 disables surface texture.
1 enables surface texture.
KANZI_SHADER_USE_TANGENT_TEXTURE. Tangent texture can replace use of vertex tangents.
0 disables tangent texture.
1 enables tangent texture.
KANZI_SHADER_USE_TEXTURE_COORDINATES. When material type has a texture, you must enable texture coordinates.
0 disables texture coordinates.
1 enables texture coordinates.
You can use these preprocessor defines for debugging. To enable debugging, set the values to 1:
These preprocessor defines are required, but they are used for features that are still in development. Always use value 0 for these: